package top.songxs.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.songxs.gmall.product.dto.SpuInfoDto;
import top.songxs.gmall.product.entity.SpuImage;
import top.songxs.gmall.product.entity.SpuInfo;
import top.songxs.gmall.product.entity.SpuSaleAttr;
import top.songxs.gmall.product.entity.SpuSaleAttrValue;
import top.songxs.gmall.product.service.SpuImageService;
import top.songxs.gmall.product.service.SpuInfoService;
import top.songxs.gmall.product.mapper.SpuInfoMapper;
import org.springframework.stereotype.Service;
import top.songxs.gmall.product.service.SpuSaleAttrService;
import top.songxs.gmall.product.service.SpuSaleAttrValueService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 86176
 * @description 针对表【spu_info(商品表)】的数据库操作Service实现
 * @createDate 2023-05-15 20:14:07
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {
    @Autowired
    private SpuImageService spuImageService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService;
    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public Page<SpuInfo> findSpuInfoByPage(Integer pageNum, Integer pageSize, Long category3Id) {

        Page<SpuInfo> spuInfoPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuInfo::getCategory3Id, category3Id);
        page(spuInfoPage, wrapper);
        return spuInfoPage;
    }

    @Override
    @Transactional
    public void saveSpuInfo(SpuInfoDto spuInfoDto) {
        //保存spu基本信息
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoDto, spuInfo);
        this.save(spuInfo);
        //保存spu的图片
        List<SpuImage> spuImageList = spuInfoDto.getSpuImageList();
        spuImageList = spuImageList.stream().peek(spuImage -> {
            spuImage.setSpuId(spuInfo.getId());
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);
        //保存spu的销售属性
        List<SpuSaleAttr> spuSaleAttrList = spuInfoDto.getSpuSaleAttrList();
        spuSaleAttrList = spuSaleAttrList.stream().peek(spuSaleAttr -> {
            spuSaleAttr.setSpuId(spuInfo.getId());
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);
        //保存spu的销售属性的值
        spuSaleAttrList.stream().forEach(spuSaleAttr -> {
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            spuSaleAttrValueList = spuSaleAttrValueList.stream().peek(spuSaleAttrValue -> {
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
            }).collect(Collectors.toList());
            spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
        });


    }
}




